package group2assignone.view.manager;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Flight;
import group2assignone.ucm.manager.*;
import group2assignone.view.OurView;
import group2assignone.view.UITool;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 *This class handles the GUI displayed when the user selects to delete a flight.
 * @author group2
 */
public class DeleteFlightView extends OurView {

    private JLabel title = new JLabel("Delete Flight");
    private JButton search = new JButton("Search");
    private JButton cancel = new JButton("Cancel");
    private JButton delete = new JButton("Delete");
    private JButton back = new JButton("Back");
    private JLabel flightNumPrompt = new JLabel("Flight Number:");
    private JLabel depLocPrompt = new JLabel("Departure Location:");
    private JLabel depTimePrompt = new JLabel("Departure Time:");
    private JLabel arrLocPrompt = new JLabel("Arrival Location:");
    private JLabel arrTimePrompt = new JLabel("Arrival Time:");
    private JLabel seatCapPrompt = new JLabel("Seating Capacity: ");
    private JLabel datePrompt = new JLabel("Date: ");
    private JLabel flightNumLabel = new JLabel("Enter Flight Number:");
    private JLabel costPrompt = new JLabel("Ticket Price:");
    private JTextField fnum = new JTextField(5);
    private JLabel flightNum = new JLabel();
    private JLabel depLoc = new JLabel();
    private JLabel depTime = new JLabel();
    private JLabel arrLoc = new JLabel();
    private JLabel arrTime = new JLabel();
    private JLabel seatCap = new JLabel();
    private JLabel date = new JLabel();
    private JLabel cost = new JLabel();
    private Flight f = new Flight();
    /*
     *Constructor.
     * Performs no function.
     */
    public DeleteFlightView() {
    }


    /*
     * Adds all of the relevant components to the view.
     * Created view will allow flight number to be entered.
     */
    public void addComponents() {

        southPanel.add(search);
        southPanel.add(cancel);

        centerPanel.add(flightNumLabel);
        centerPanel.add(fnum);

        pack();
        search.addActionListener(new UCCtoALAdapter(new UCDeleteFlightSearch(this, f)));
        cancel.addActionListener(new UCCtoALAdapter(new UCCancelAction(this)));
        delete.addActionListener(new UCCtoALAdapter(new UCDeleteFlight(this, f)));
        back.addActionListener(new UCCtoALAdapter(new UCDeleteFlightBack(this)));
    }

    /*
     * Loads details in text fields into Flight f object.
     * @param Flight f
     */
    public void loadDetails(Flight f) {
        f.setFlightNum(flightNum.getText());
        f.setArrLoc(arrLoc.getText());
        f.setArrTime(arrTime.getText());
        f.setDepLoc(depLoc.getText());
        f.setDepTime(depTime.getText());
        if (!seatCap.getText().equals("")) {
            f.setSeatCap(Integer.parseInt((seatCap.getText())));
        }
        f.setDate(date.getText());
        f.setCost(cost.getText());
    }

    /*
     * Removes the components used for the Flight Num GUI
     */
    public void clearFlightNumWindow() {
        remove(centerPanel);
        southPanel.removeAll();
    }

    /*
     * Removes the components used for the flight display window
     */
    public void clearShowFlightWindow() {
        westPanel.removeAll();
        eastPanel.removeAll();
        southPanel.removeAll();
        remove(westPanel);
        remove(eastPanel);
    }

    /*
     * Returns the entered flight number
     */
    public String getFlightNum() {

        return fnum.getText();
    }

    /*
     * Adds the components required to show the flight details.  Flight details
     * are obtained from Flight f.
     * @param Flight f
     */
    public void showFlightDetails(Flight f) {
        //Initialise components
        clearFlightNumWindow();
        //Initialise components

        eastPanel.setLayout(new GridLayout(4, 2, 2, 1));
        westPanel.setLayout(new GridLayout(4, 2, 2, 1));

        flightNum.setText(f.getFlightNum());
        depLoc.setText(f.getDepLoc());
        arrLoc.setText(f.getArrLoc());
        seatCap.setText("" + f.getSeatCap()); //turn seatcap into string
        date.setText(f.getDate());
        depTime.setText(f.getDepTime());
        arrTime.setText(f.getArrTime());

        westPanel.add(flightNumPrompt);
        westPanel.add(flightNum);
        westPanel.add(depLocPrompt);
        westPanel.add(depLoc);
        westPanel.add(arrLocPrompt);
        westPanel.add(arrLoc);
        westPanel.add(seatCapPrompt);
        westPanel.add(seatCap);

        eastPanel.add(datePrompt);
        eastPanel.add(date);
        eastPanel.add(depTimePrompt);
        eastPanel.add(depTime);
        eastPanel.add(arrTimePrompt);
        eastPanel.add(arrTime);
        eastPanel.add(costPrompt);
        eastPanel.add(cost);

        eastPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 10));
        westPanel.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));

        southPanel.add(delete);
        southPanel.add(back);
        southPanel.add(cancel);

        pack();

    }

    /*
     * Resets the flightNum text field to be empty.
     */
    public void resetFlightNum() {
        //Reset flight num field for re-entry
        flightNum.setText("");
    }

    /*
     * Displays error message notifying user of invalid flight number.
     */
    public void errorFlightNum() {
        //Show error dialogue box - Flight does not exist
        UITool.displayError("Invalid Flight Number.  Please re-enter.", this);
        resetFlightNum();
    }
}